package com.example.application.views.count;

import java.io.IOException;
import java.time.LocalDate;
import java.util.*;

import com.example.application.bean.ResultBean;
import com.example.application.bean.Sales;
import com.example.application.bean.StoreCount;
import com.example.application.bean.User;
import com.example.application.views.util.DialogUtil;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.vaadin.flow.component.Component;
import com.vaadin.flow.component.UI;
import com.vaadin.flow.component.board.Board;
import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.charts.Chart;
import com.vaadin.flow.component.charts.model.ChartType;
import com.vaadin.flow.component.charts.model.Configuration;
import com.vaadin.flow.component.charts.model.Crosshair;
import com.vaadin.flow.component.charts.model.ListSeries;
import com.vaadin.flow.component.charts.model.XAxis;
import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.router.Route;
import com.vaadin.flow.router.PageTitle;
import com.example.application.views.main.MainView;
import com.vaadin.flow.component.dependency.CssImport;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

@Route(value = "count", layout = MainView.class)
@PageTitle("统计")
@CssImport("./views/统计/统计-view.css")
public class CountView extends Div {

    private Chart monthlyVisitors = new Chart();

    private List<StoreCount> storeCounts;

    public CountView() {
        User user = (User) UI.getCurrent().getSession().getAttribute("user");
        if (user == null){

        }else if("门店管理员".equals(user.getIdentity()) || "供应商管理员".equals(user.getIdentity())){

            this.init();
        }else{
            DialogUtil.adminAuthority();
        }
    }

    private void populateCharts() {

        storeCounts = getResult().getT();

        Map<String, Double> map = new HashMap<>();
        for (StoreCount storeCount : storeCounts) {
            map.put(storeCount.getName(), storeCount.getPrice());
        }

        List<Sales> salesList = new ArrayList<>();

        Set<String> strings = map.keySet();
        for (String string : strings) {
            Sales sales = new Sales();
            sales.setName(string);
            sales.setPrice(map.get(string));
            salesList.add(sales);
        }

        for (StoreCount storeCount : storeCounts) {
            for (Sales sales : salesList) {
                if(storeCount.getName().equals(sales.getName())){
                    sales.getMonthSale()[storeCount.getCreateTime().getMonthValue() - 1] = storeCount.getNum();
                }
            }
        }

        // First chart
        Configuration configuration = monthlyVisitors.getConfiguration();
        for (Sales sales : salesList) {
            configuration.addSeries(new ListSeries(sales.getName(), sales.getMonthSale()[0], sales.getMonthSale()[1], sales.getMonthSale()[2], sales.getMonthSale()[3],  sales.getMonthSale()[4], sales.getMonthSale()[5], sales.getMonthSale()[6], sales.getMonthSale()[7], sales.getMonthSale()[8], sales.getMonthSale()[9], sales.getMonthSale()[10], sales.getMonthSale()[11]));
        }

        XAxis x = new XAxis();
        x.setCrosshair(new Crosshair());
        x.setCategories("一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月");
        configuration.addxAxis(x);

    }

    public void init(){

        storeCounts = getResult().getT();

        addClassName("统计-view");
        Board board = new Board();

        monthlyVisitors.getConfiguration().setTitle("每月商品销量");
        monthlyVisitors.getConfiguration().getChart().setType(ChartType.COLUMN);
        WrapperCard monthlyVisitorsWrapper = new WrapperCard("wrapper", new Component[]{monthlyVisitors}, "card");
        board.add(monthlyVisitorsWrapper);

        add(board);
        populateCharts();
    }

    public ResultBean<List<StoreCount>> getResult(){

        ResultBean<List<StoreCount>> resultBean = null;

        try (CloseableHttpClient httpClient = HttpClients.createDefault()){

            HttpGet httpGet = new HttpGet("http://localhost:8081/pet/store/showStoreSalesVolumeForDate/2");
            CloseableHttpResponse httpResponse = httpClient.execute(httpGet);
            String json = EntityUtils.toString(httpResponse.getEntity());
            ObjectMapper mapper = new ObjectMapper();
            resultBean = mapper.readValue(json, new TypeReference<>() {
            });
        } catch (IOException e) {
            e.printStackTrace();
        }

        return resultBean;
    }
}
